<template>
  <div class="app-container">



    <div class="filter-container">
        <el-button class="filter-item" type="primary" icon="el-icon-refresh" @click=" getList()">刷新机构</el-button>
        <el-button class="filter-item" type="primary" icon="el-icon-refresh" @click="changeExpandState()">展开|折叠</el-button>
        <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate(null)">新增一级部门</el-button>
    </div>

    <tree-table :data="treeTableDataList" :columns="columns"
       :expand-all="expandAll" :listLoading="listLoading" border  ref="organTree"
        rowKey="deptId"   >
        <el-table-column label="操作" width="270" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleCreate(scope.row.deptId)">新增子级部门</el-button>
            <el-button type="published" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
    </tree-table>


    <!-- 新增修改界面 -->
    <el-dialog  :title="operation?'新增':'编辑'"  width="40%" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
        <el-form :model="dataForm" :rules="dataFormRules" ref="dataForm" @keyup.enter.native="saveFormData()"
                 label-width="100px" size="small" style="text-align:left;">

          <el-form-item label="部门名称" prop="deptName">
            <el-input v-model="dataForm.deptName" placeholder="请输入部门名称"></el-input>
          </el-form-item>
          <el-form-item  label="部门负责人" prop="leader">
            <el-input v-model="dataForm.leader" placeholder="请输入部门负责人"></el-input>
          </el-form-item>
          <el-form-item  label="负责人电话" prop="phone">
            <el-input v-model="dataForm.phone" placeholder="请输入负责人电话"></el-input>
          </el-form-item>
          <el-form-item  label="负责人邮箱" prop="email">
            <el-input v-model="dataForm.email" placeholder="请输入负责人邮箱"></el-input>
          </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button size="small"  @click="closeDialog()">取消</el-button>
          <el-button size="small"  @click="saveFormData()"   :loading="editLoading"   type="primary"  >确定</el-button>
        </span>
    </el-dialog>

  </div>


</template>

<script>
/**
  Auth: chenyue1993
  Created: 2019/11/14-14:54
*/
import treeTable from '@/components/TreeTable'
export default {
  name: 'system-organ',
  components: { treeTable },
  data() {
    return {

      treeTableDataList: [],
      listLoading:true,
      expandAll: false, // 默认不展开所有节点
      columns: [
        // { text: 'ID', value: 'deptId',  width: '320px',align:'center' },
        { text: '部门名称',  value: 'deptName', align:'center' },
        { text: '负责人', value: 'leader',align:'center' },
        { text: '负责人电话',  value: 'phone',align:'center' },
        { text: '创建时间', value: 'createTime',align:'center' }
      ],


      operation :true ,//默认标记新增
      dialogFormVisible: false,
      editLoading :false,
      dataForm:{},
      dataFormRules: {
          deptName: [ { required: true, message: '请输入部门名称', trigger: 'blur' } ],
          leader: [ { required: true, message: '请输入负责人', trigger: 'change' } ],
          phone: [ { required: true, message: '请输入负责人电话', trigger: 'change' } ]
      },
    }
  },
  methods: {

    // 改变展开收缩
    changeExpandState() {
      this.expandAll = !this.expandAll
      if(this.expandAll){
          this.$refs.organTree.expandAllRow();
        }else{
          this.$refs.organTree.collapseAllRow();
        }
        this.$forceUpdate();
    },

    //获取数据
    getList() {
        this.listLoading = true
        //【API请求】---- 获取组织机构树数据
        this.$api.getOrganList(this.listQuery).then(response => {
          this.treeTableDataList = response.data
          this.listLoading = false;
        })
    },
    //【按钮点击】--- 弹出新增表单
    handleCreate(pid) {
        this.operation = true;
        this.initDataForm();
        if(pid){
            this.dataForm.parentId = pid
        }
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
          this.editLoading = false;
        })
    },
    //【按钮点击】--- 弹出编辑表单
    handleUpdate(row) {
        this.operation = false;
        //this.dataForm = Object.assign({}, row)
        this.dataForm =   {
            deptId: row.deptId,
            parentId: row.parentId,
            deptName: row.deptName,
            leader: row.leader ,
            phone: row.phone,
            email: row.email ,
        },
        this.dialogFormVisible = true
        this.$nextTick(() => {
            this.$refs['dataForm'].clearValidate()
            this.editLoading = false;
        })

    },
    //保存表单数据
    saveFormData() {
        this.$refs['dataForm'].validate((valid) => {
            if (!valid) {
              return false;
            }
            this.editLoading = true;
            //【API请求】----保存表单数据
            this.$api.saveOrganInfo(this.dataForm).then(() => {
                this.editLoading = false;//关闭loading框
                this.dialogFormVisible = false;//关闭表单页面
                this.initDataForm;//重置表单数据
                this.getList();//重新获取表格数据
                this.$notify({ title: '成功',  message: '保存成功', type: 'success', duration: 2000 })
            })
        })
    },
    //【按钮点击】---删除行
    handleDelete(row) {
        if(row.children.length>0){
            this.$message({ message: '该部门下存在下级部门，不可删除', type: 'error' })
            return ;
        }
        this.$confirm('确认删除该记录吗？', '提示', {}).then(() => {
            //【API请求】---- 删除一行数据
            this.$api.deleteOrgan({ids:row.deptId}).then(() => {
                this.$message({  message: '操作成功', type: 'success' })
                this.pageRequest.pageNum = 1
                this.getList();
            })
        }).catch(() => {

        });
    },

    //初始化表单数据，重新渲染
    initDataForm(){
        this.dataForm = {
            deptId: '',
            parentId: '',
            deptName: '',
            leader:'',
            phone: '',
            email: '',
        }
    },
    //关闭表单弹出框
    closeDialog(){
        //this.$refs['dataForm'].resetFields();
        this.initDataForm();
        this.dialogFormVisible = false;
    }



  },
  created() {
      this.getList()
  },
}
</script>
